/*
 * ContentPaneDelegate.cpp  0.4.0 / Nov 30, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ContentPaneDelegate.h"

#include "ContentPane.h"

// ContentPaneDelegate :: Protected Constructor. -------------------------------

/*!
 * \brief Constructs new ContentPaneDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ContentPane object
 */
ContentPaneDelegate::ContentPaneDelegate(JNIEnv *env, jobject obj)
  : ContainerDelegate(env, obj, new ContentPane(this))
{
  // empty constructor body
}

// ContentPaneDelegate :: Static Native Calls. ---------------------------------

/*!
 * \brief Initializes ContentPaneDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the ContentPane java class
 *
 * \exception FatalError if failed
 */
void ContentPaneDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_getLayoutPreferredSizeMID);
  if (!INIT_UMID(getLayoutPreferredSize, cls,
                "()Lcom/cute4j/swing/graphics/Dimension;")) {
    FATAL_LOGGED_ERROR("ContentPaneDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ContentPaneDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ContentPaneDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// ContentPaneDelegate :: Native Callbacks. ------------------------------------

/*!
 * \brief Returns layout's preferred size.
 */
QSize ContentPaneDelegate::getLayoutPreferredSize()
{
  jobject preferredSize;
  if (jniObject()->callObjectMethod(preferredSize,
                                    sm_getLayoutPreferredSizeMID)) {
    jint preferredWidth;
    jint preferredHeight;
    DimensionObject preferredSizeDimension(jniObject()->env(), preferredSize);
    if (preferredSizeDimension.getWidth(preferredWidth) &&
        preferredSizeDimension.getHeight(preferredHeight)) {
      return QSize(preferredWidth, preferredHeight);
    }
  }
  return QSize();
}

// ContentPaneDelegate :: Private Static Data Members. -------------------------

jmethodID ContentPaneDelegate::sm_getLayoutPreferredSizeMID = 0;
